<%@ page import="list.control.Guest" %>
<%@ page import="list.model.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    User user = new User();
    if (!user.init(Guest.getUser(session.getId()))) {
        response.sendRedirect("../index.jsp?opt=login&val=failed");//初始化失败则返回登录页面
    }
    String tagTemp = request.getParameter("tag");
    int tag = (tagTemp == null) ? 0 : Integer.parseInt(tagTemp);
    Contact[] output = null;
    if (tag > 0) {
        output = user.getContacts(tag);
    } else if (tag == -1) {
        String val = request.getParameter("val");
        String pro = request.getParameter("pro");
        switch (pro) {
            case "name":
                output = (Contact.searchName(user.getUser_id(), val, 20));
                break;
            case "nickname":
                output = (Contact.searchNickname(user.getUser_id(), val, 20));
                break;
            case "email":
                output = (Contact.searchEmail(user.getUser_id(), val, 20));
                break;
            case "birthday":
                output = (Contact.searchBirthday(user.getUser_id(), val, 20));
                break;
            case "work_place":
                output = (Contact.searchWork_Place(user.getUser_id(), val, 20));
                break;
            case "postcode":
                output = (Contact.searchPostcode(user.getUser_id(), val, 20));
                break;
            case "address":
                output = (Contact.searchAddress(user.getUser_id(), val, 20));
                break;
            case "wechat":
                output = (Contact.searchWechat(user.getUser_id(), val, 20));
                break;
            case "qq":
                output = (Contact.searchQq(user.getUser_id(), val, 20));
                break;
            case "alipay":
                output = (Contact.searchAlipay(user.getUser_id(), val, 20));
                break;
        }
    } else {
        output = user.getContacts();
    }
%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AddressList</title>
    <link rel="stylesheet" type="text/css" href="style/reset.css">
    <link rel="stylesheet" type="text/css" href="style/style.css">
    <link rel="stylesheet" href="style/bootstrap.min.css">
    <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/main.js"></script>
    <style>
        #search_type {
            height: 32px;
            background-color: #3c85c0;
            border-color: #3c85c0;
            color: white;
        }
    </style>
</head>
<body>
<header>
    <div id="title">通讯录</div>
    <a style="margin-top: 20px; color: #b8e3f5;" class="btn btn-lg"
       data-toggle="modal" data-target="#my" id="about_me">
        <span class="glyphicon glyphicon-user"></span> 我的信息
        <input type="hidden" id="tag" value="<%=tag%>">
    </a>
    <%--我的信息模态框模块--%>
    <div class="modal fade" id="my" tabindex="-1" role="dialog"
         aria-labelledby="me" aria-hidden="true">
        <div class="modal-dialog" style="width: 350px;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-hidden="true">×
                    </button>
                    <h4 class="modal-title" id="me">
                        我的信息
                    </h4>
                </div>
                <form action="module/route.jsp?opt=update&key=user&tag=<%=tag%>" method="post">
                    <div class="modal-body">
                        <label class="input-group">邮箱</label>
                        <input class="form-control" type="text" value="<%=user.getEmail()%>" name="email">
                        <label class="input-group">生日</label>
                        <input class="form-control" type="date" value="<%=user.getBirthday()%>" name="birthday">
                        <label class="input-group">手机</label>
                        <input class="form-control" type="text" value="<%=user.getPhone()%>" name="phone">
                    </div>
                    <div class="modal-footer">
                        <a href="module/user.jsp?opt=logout">
                            <button class="btn btn-danger pull-left">注销</button>
                        </a>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <input type="submit" class="btn btn-primary" value="保存">
                    </div>
                </form>
            </div>
        </div>
    </div>
    <%--搜索模块--%>
    <form id="search" action="./index.jsp?tag=-1" class="input-group col-md-3">
        <input id="search_in" type="text" name="val" class="form-control" placeholder="搜索内容">
        <div class="input-group-addon" style="padding: 0;border-color: #3c85c0;">
            <select id="search_type" name="pro">
                <option value="name">姓名</option>
                <option value="nickname">备注</option>
                <option value="email">邮箱</option>
                <option value="birthday">生日</option>
                <option value="work_place">工作单位</option>
                <option value="postcode">邮编</option>
                <option value="address">地址</option>
                <option value="wechat">微信</option>
                <option value="qq">QQ</option>
                <option value="alipay">支付宝</option>
            </select>
        </div>
        <span class="input-group-addon glyphicon glyphicon-search addon btn btn-info" id="search_now"></span>
    </form>
</header>
<%--信息表--%>
<section class="container-fluid">
    <aside class="col-sm-2 col-xs-12">
        <nav class="group">
            <%@ include file="module/tagList.jsp" %>
        </nav>
    </aside>
    <article class="col-sm-10 col-xs-12">
        <%@ include file="module/contactList.jsp" %>
        <div id="search_result">
        </div>
    </article>
</section>
<script>
    $(function () {
        $('#my').modal('hide');

        $("#about_me").hover(function () {
            $(this).css("color", "white");
        }, function () {
            $(this).css("color", "#b8e3f5");
        });
        $("#search_in").hover(function () {
            $(this).animate({width: "+=100px"});
        }, function () {
            $(this).animate({width: "-=100px"});
        });

        $("#search_now").click(function () {
            let val = $("#search_in").val();
            if (val !== "") {
                let pro = $("#search_type").val();
                window.location.href = "main.jsp?tag=-1&val=" + val + "&pro=" + pro;
            }
        });
    });
</script>
</body>
</html>
